package com.company.tree;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class TreePath {


    public List<String> binaryTreePaths(TreeNode root) {
        List<String> pathList = new ArrayList<>();
        travel(root, "", pathList);
        Set<String> set = new HashSet<>(pathList);
        pathList.clear();
        pathList.addAll(set);
        return pathList;
    }


    private void travel(TreeNode root, String paths, List<String> pathList) {
        if (paths!=null&&paths!="") {
            paths = paths+"->"+root.val;
        }else {
            paths = paths+root.val;
        }
        if (root.left==null&&root.right==null) {

            pathList.add(paths);
            return;
        }

        if (root.left != null) {
            travel(root.left,paths , pathList);
        }
        if (root.right != null) {
            travel(root.right, paths, pathList);
        }
    }
}
